public class Chromosome
{
   Gene[] genes;
   String genomeName;
   String chromosomeFileName;
   int chrNum;
   
   int chrLength;
   float chrX1, chrX2, chrY1, chrY2;
   
   Chromosome ( String genomeName, int chrNum, int chrLength, float chrX1, float chrY1, float chrX2, float chrY2 ){
     println( genomeName + " " + chrNum + " " + chrLength + " " + chrX1 + " " + chrY1 + " " + chrX2 + " " + chrY2);
     
     this.genomeName = genomeName;
     this.chrNum = chrNum;
     this.chrLength = chrLength;
     this.chrX1 = chrX1;
     this.chrY1 = chrY1;
     this.chrX2 = chrX2;
     this.chrY2 = chrY2;
     
     chromosomeFileName = genomeName+"Chr"+chrNum+".ptt"; 
     
     //READ FILE AND GET CHROMOSOME INFO
     String[] rows = loadStrings(chromosomeFileName);
    
     String[] columns = split(rows[0], '\t');
     String[] columnNames = subset(columns, 1); // upper-left corner ignored
     scrubQuotes(columnNames);
     int columnCount = columnNames.length;
      
     String[] rowNames = new String[rows.length-1];
     String[][] data = new String[rows.length-1][];
      
     int rowCount = 0;
      
     // start reading at row 1, because the first row was only the column headers
     for (int i = 1; i < rows.length; i++) {
        if (trim(rows[i]).length() == 0) {
          continue; // skip empty rows
        }
        if (rows[i].startsWith("#")) {
          continue;  // skip comment lines
        }

        // split the row on the commas
        String[] pieces = split(rows[i], '\t');
        scrubQuotes(pieces);
      
        // copy row title
        rowNames[rowCount] = pieces[0];
        // copy data into the table starting at pieces[1]
        data[rowCount] = subset(pieces, 1);

        // increment the number of valid rows found so far
        rowCount++;      
     }
      
     // resize the 'data' array as necessary
     data = (String[][]) subset(data, 0, rowCount);
       
     //read from data array to create gene array 
     genes = new Gene[ data.length-3 ]; //one gene per row
     int prevChr = 1;
     for(int i = 3; i < data.length; i++) //skip the first 
     {
        int startNucleotide = parseStartNucleotide( rowNames[i] );
        int endNucleotide = parseEndNucleotide( rowNames[i] );
        boolean strand = parseStrand ( data[i][0] );
        int geneLength = parseInt(data[i][1]);
        int pid = parseInt( data[i][2] );
        String geneName = data[i][3];
        String synonym = data[i][4];
        String code = data[i][5];
        String cog = data[i][6];
        String product = data[i][7];
        
        float geneX1 = findGeneX1( startNucleotide );
        float geneX2 = findGeneX2( endNucleotide );
        float geneY1 = chrY1;
        float geneY2 = chrY2;
         
        genes[i-3] = new Gene( geneX1, geneY1, geneX2, geneY2, startNucleotide, endNucleotide, strand, geneName, synonym, product );
     }
     
     println( "chr " + chrNum + " has " + genes.length + " genes " );
  }//end constructor
  
  void draw()
  {
    println("drawing chr " + chrNum );
    rectMode(CORNERS);
    fill( 0xFF5386D4 );
    
    //rect(chrX1, chrY1, chrX2, chrY2);
    
    for(int i = 0; i < genes.length; i++)
      genes[i].draw();
  }
  
  int parseStartNucleotide( String st )
  {
    String[] loc = split( st, "." );
    //println("loc[0] = " + loc[0] + " loc[2] = " + loc[2] );
    return parseInt(loc[0]);
  }
  
  int parseEndNucleotide( String st )
  {
    String[] loc = split( st, "." );
    //println("loc[0] = " + loc[0] + " loc[2] = " + loc[2] );
    return parseInt(loc[2]);
  }
  
  boolean parseStrand( String st )
  {
    if( st.compareTo("+") == 0)
      return true;
    return false;
  }
  
  float findGeneX1( int startNT )
  {
    //  geneDistFrom0/distInCoord = startNT/chrLength
    return ( (float)startNT/ (float)chrLength) * (float)(chrX2 - chrX1)+chrX1;
  }
  
  float findGeneX2( int endNT )
  {
    return ( (float)endNT / (float)chrLength ) * (float)(chrX2 - chrX1)+chrX1;
  }
  
  void scrubQuotes(String[] array) {
    for (int i = 0; i < array.length; i++) {
      if (array[i].length() > 2) {
        // remove quotes at start and end, if present
        if (array[i].startsWith("\"") && array[i].endsWith("\"")) {
          array[i] = array[i].substring(1, array[i].length() - 1);
        }
      }
      // make double quotes into single quotes
      array[i] = array[i].replaceAll("\"\"", "\"");
    }
  }//end scrubQuotes
  
}
